/**
 * @Title:OperatorAction.java
 * CopyRight(c)2010-DMX
 */
package com.dmx.fw.web.struts2.action.system;

import java.io.IOException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.dmx.fw.bean.mapping.pojo.Operator;
import com.dmx.fw.bean.mapping.pojo.Role;
import com.dmx.fw.service.interfaces.OperatorService;
import com.dmx.fw.web.struts2.action.BaseAction;
import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.exception.GeneralException;
import com.dmx.fw.exception.GeneralExceptionHandler;
import com.opensymphony.xwork2.ActionContext;
/**
 * @Description: Operator Manager
 * @author Ma Qiang
 * @date 2010-4-29
 * @version V1.0
 */
public class OperatorAction extends BaseAction {
	final private static Log log = LogFactory.getLog(RoleAction.class);
	/**
	 * @Fields operator : 操作员对象
	 */
	private Operator operator;
	/**
	 * @Fields operType : 操作员类型
	 */
    private String operType;
    /**
	 * @Fields roleArray : 已选择的角色
	 */
	private String[] roleArray;
	/**
	 * @Fields roleList : 进入新增、修改页时取出所有角色供用户选择，listKey、listValue用到
	 */
	private List roleList;
	/**
	 * @Fields roleIdArray : 已选中的角色id，s:checkboxlist中的value必须与listKey中的类型一致，将roleArray转成Long型
	 */
	private Long[] roleIdArray;
	/**
	 * @Fields roleView : 查看时显示的角色名称
	 */
	private String roleView;
	/**
	 * @Fields operatorTypeName : 查看时显示的操作员类型名称
	 */
	private String operatorTypeName;
	/**
	 * @Fields oldLoginName : 旧登录名，修改时用到
	 */
	private String oldLoginName;
	/**
	 * @Fields operatorService : OperatorService
	 */
	private OperatorService operatorService;
	/**
	 * @Fields listOperatorType : 操作员类型下拉框
	 */
	private List listOperatorType;

	/**
	 * 原密码，修改时用到
	 */
	private String oldPwd;	
	
	/**
	 * 新密码，修改时用到
	 */
	private String newPwd;

	public String execute() throws Exception {
		return super.execute();
	}
	/**
	 * @Description: 进入添加输入界面
	 * @param 
	 * @return String
	 */
	public String doAddInput() {
		addList();
		this.operType = getOperatorType();
		return "addInput";
	}
	/**
	 * @Description: 执行添加提交操作
	 * @param 
	 * @return String
	 */
	public String doAddSubmit() throws IOException {

		operator.setStatus("1");
		//检查登录名是否重复
		if (operatorService.checkLoginNameUnique(operator.getLogin())) {
			this.setErrorInfo(getText("operator.loginNameDuplicate"));
			return doAddInput();
		} else {
			operatorService.save(operator, roleArray);
			this.setSuccessinfo(this.getText("info.addSuccess"));
			//this.setAjaxJsonUrlResult("system/operatorSearch.action?act=query&busiId=searchOperator");
			return "list";
		}
	}
	/**
	 * @Description: 进入修改输入界面
	 * @param 
	 * @return String
	 */
	public String doEditInput() {
		addList();
		this.operType = getOperatorType();
		operator = operatorService.getOperator(operator.getId());
		//取得已选择角色列表
		List temp = operatorService.getRolesById(operator.getId());
		if (temp != null && !temp.isEmpty()) {
			roleIdArray = new Long[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				Role role = (Role) temp.get(i);
				roleIdArray[i] = Long.parseLong(String.valueOf(role==null?"0":role.getId()));//默认选中listKey的类型和value的类型要一致
			}
		}
		return "addInput";
	}
	/**
	 * @Description: 执行修改提交界面
	 * @param 
	 * @return String
	 */
	public String doEditSubmit() throws IOException {
		//检查登录名是否重复
		if (!oldLoginName.equalsIgnoreCase(operator.getLogin())&& operatorService.checkLoginNameUnique(operator.getLogin())) {
			this.setErrorInfo(getText("operator.loginNameDuplicate"));
			return doEditInput();
		} else {
			operatorService.update(operator, roleArray);
			this.setSuccessinfo(this.getText("info.modifySuccess"));
			return "list";
		}
	}
	/**
	 * @Description: 执行删除操作（状态更新）
	 * @param 
	 * @return String
	 */
	public String doDelete() throws Exception {
		operatorService.removeOperator(operator);
		this.setSuccessinfo(this.getText("info.deleteSuccess"));
		//this.setAjaxJsonUrlResult("system/operatorSearch.action?act=query&busiId=searchOperator");
		return "list";
	}
	/**
	 * @Description: 执行查看动作
	 * @param 
	 * @return String
	 */
	public String doView() {
		addList();
		operator = operatorService.getOperator(operator.getId());
		//取得已选择角色列表
		List temp = operatorService.getRolesById(operator.getId());

		if (temp != null && !temp.isEmpty()) {
			roleArray = new String[temp.size()];
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < temp.size(); i++) {
				Role role = (Role) temp.get(i);
				sb.append(String.valueOf(role.getName().substring(5)));
				sb.append(",");
			}
			roleView = sb.substring(0, sb.length()-1).toString();
			operatorTypeName = operatorService.getOperatorTypeName(operator.getOperatorType());
		}
		return "addInput";
	}
	
	public String changePsw() throws Exception{
		
		return "changePsw";
	}
	
	public String doChangePsw() throws Exception{
		
		try {
			Operator _operator = operatorService.getOperator(getCurrOperator()
					.getLogin());

			if (!_operator.getPassword().equals(oldPwd)) {
				this.setErrorInfo(this
						.getText("operator.validate.oldPwd.error"));
			} else {
				_operator.setPassword(newPwd);
				operatorService.changePsw(_operator);
				this.setSuccessinfo(this.getText("info.modifySuccess"));
			}
		} catch (Exception e) {
			log.error(e);
			GeneralException ge = GeneralExceptionHandler.Handle(e);
			this.setErrorInfo(this.getText(ge.getMessage()));
		}
		return "changePsw";
	}
	
	//
	/**
	 * @Description: 进入添加、或修改页面时初始化操作员类型的下拉、角色的复选框
	 * @param 
	 * @return String
	 */
	private void addList() {
		// listOperatorType = operatorService.getOperatorType();
		roleList = operatorService.getAllRolesArray();
		for (int i = 0; roleList != null && i < roleList.size(); i++) {
			Role role=(Role)roleList.get(i);
			role.setName(role.getName().substring(5));
		}
	}
	/**
	 * @return the operator
	 */
	public Operator getOperator() {
		return operator;
	}
	/**
	 * @param operator
	 *            the operator to set
	 */
	public void setOperator(Operator operator) {
		this.operator = operator;
	}
	/**
	 * @return the operatorService
	 */
	public OperatorService getOperatorService() {
		return operatorService;
	}
	/**
	 * @param operatorService
	 *            the operatorService to set
	 */
	public void setOperatorService(OperatorService operatorService) {
		this.operatorService = operatorService;
	}
	/**
	 * @return the roleArray
	 */
	public String[] getRoleArray() {
		return roleArray;
	}

	/**
	 * @param roleArray
	 *            the roleArray to set
	 */
	public void setRoleArray(String[] roleArray) {
		this.roleArray = roleArray;
	}
	/**
	 * @Description: 取得当前操作员类型
	 * @return String
	 */
	protected String getOperatorType() {
		return getCurrOperator().getOperatorType();
	}
	
	
	/**
	 * @return the oldLoginName
	 */
	public String getOldLoginName() {
		return oldLoginName;
	}
	/**
	 * @param oldLoginName
	 *            the oldLoginName to set
	 */
	public void setOldLoginName(String oldLoginName) {
		this.oldLoginName = oldLoginName;
	}

	public String getOperType() {
		return operType;
	}

	public void setOperType(String operType) {
		this.operType = operType;
	}

	public List getListOperatorType() {
		return listOperatorType;
	}

	public void setListOperatorType(List listOperatorType) {
		this.listOperatorType = listOperatorType;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public Long[] getRoleIdArray() {
		return roleIdArray;
	}

	public void setRoleIdArray(Long[] roleIdArray) {
		this.roleIdArray = roleIdArray;
	}

	public String getRoleView() {
		return roleView;
	}

	public void setRoleView(String roleView) {
		this.roleView = roleView;
	}

	public String getOperatorTypeName() {
		return operatorTypeName;
	}

	public void setOperatorTypeName(String operatorTypeName) {
		this.operatorTypeName = operatorTypeName;
	}
	public String getNewPwd() {
		return newPwd;
	}
	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
	public String getOldPwd() {
		return oldPwd;
	}
	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

}
